package ufs.dcomp.mgemeos.dao;

import java.net.URLDecoder;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import ufs.dcomp.mgemeos.negocio.Horario;

/**
 *
 * @author Luiz Marcus
 * @modificacao 20/02/11
 */
public class HorarioDAO implements interface_dao.IHorario {

    private static String inserir = "insert into horario (hora_inicio, hora_fim,dia,data,idLocal,idExposicao) values (?,?,?,?,?,?)";
    private static String atualizar = "update horario set hora_inicio=?, hora_fim=?,dia=?,data=?,idLocal=? where idExposicao=?";
    private static String obter_local_horario = "select * from horario where idLocal = ? ";
    private static String obter_exposicao_horario = "select * from horario where idExposicao = ? ";
    private static String delete = "delete from horario where idHorario = ? ";

    @Override
    public void atualizarHorario(Horario horario) {
        Conexao conexao = new Conexao();
        Connection con = conexao.getConnection();
        PreparedStatement pst;
        try {
            pst = con.prepareStatement(atualizar);
            try {
                pst.setTime(1, formataHora(horario.getHora_inicio()));
            } catch (Exception ex) {
                Logger.getLogger(HorarioDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                pst.setTime(2, formataHora(horario.getHora_fim()));
            } catch (Exception ex) {
                Logger.getLogger(HorarioDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
            pst.setString(3, horario.getDia());
            try {
                pst.setDate(4, formataData(horario.getData()));
            } catch (Exception ex) {
                Logger.getLogger(HorarioDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
            pst.setInt(5, horario.getIdLocal());
            pst.setInt(6, horario.getIdExposicao());
            pst.execute();
            pst.close();
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();

        }
    }

    @Override
    public Horario obter_Local_Horario(int id) {
        Horario horario = null;
        Conexao conexao = new Conexao();
        Connection con = conexao.getConnection();
        
        PreparedStatement pst;
        try {
            pst = con.prepareStatement(obter_local_horario);
            pst.setInt(1, id);
            ResultSet rs = pst.executeQuery();
            if (rs.next()) {

                horario = new Horario();
                horario.setIdHorario(rs.getInt(1));
                horario.setHora_inicio(rs.getTime(2).toString());
                horario.setHora_fim(rs.getTime(3).toString());
                horario.setDia(rs.getString(4));
                horario.setData(rs.getDate(5).toString());
                horario.setIdLocal(rs.getInt(6));
                horario.setIdExposicao(rs.getInt(7));

            }
            rs.close();
            pst.close();
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return horario;
    }

    @Override
    public ArrayList<Horario> obter_Exposicao_Horario(int id) {
        ArrayList<Horario> horarios = new ArrayList<Horario>();
        Horario horario = null;
        Conexao conexao = new Conexao();
        Connection con = conexao.getConnection();
        PreparedStatement pst;
        try {
            pst = con.prepareStatement(obter_exposicao_horario);
            pst.setInt(1, id);
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {

                horario = new Horario();
                horario.setIdHorario(rs.getInt(1));
                horario.setHora_inicio(rs.getTime(2).toString());
                horario.setHora_fim(rs.getTime(3).toString());
                horario.setDia(rs.getString(4));
                horario.setData(rs.getDate(5).toString());
                horario.setIdLocal(rs.getInt(6));
                horario.setIdExposicao(rs.getInt(7));
                horarios.add(horario);
            }
            rs.close();
            pst.close();
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return horarios;
    }

    @Override
    public boolean inserirHorario(Horario horario) {
        Conexao conexao = new Conexao();
        Connection con = conexao.getConnection();
        PreparedStatement pst;

        try {
            pst = con.prepareStatement(inserir);
            

                try {
                    pst.setTime(1, formataHora(URLDecoder.decode(horario.getHora_inicio(), "UTF-8")));
                } catch (Exception ex) {
                    Logger.getLogger(HorarioDAO.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    pst.setTime(2, formataHora(URLDecoder.decode(horario.getHora_fim(), "UTF-8")));
                } catch (Exception ex) {
                    Logger.getLogger(HorarioDAO.class.getName()).log(Level.SEVERE, null, ex);
                }
                pst.setString(3, horario.getDia().toString());
                try {
                    pst.setDate(4, formataData(URLDecoder.decode(horario.getData(), "UTF-8")));
                } catch (Exception ex) {
                    Logger.getLogger(HorarioDAO.class.getName()).log(Level.SEVERE, null, ex);
                }
                pst.setInt(5, horario.getIdLocal());
                pst.setInt(6, horario.getIdExposicao());
                pst.execute();
            
            pst.close();
            con.close();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }

    }

    @Override
    public boolean deletarHorario(int id) {
        Conexao conexao = new Conexao();
        Connection con = conexao.getConnection();
        PreparedStatement pst;

        try {
            pst = con.prepareStatement(delete);
            pst.setInt(1, id);
            pst.execute();
            pst.close();
            con.close();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }

    }

    
    public static java.sql.Date formataData(String data) throws Exception {
        if (data == null || data.equals("")) {
            return null;
        }

        java.sql.Date date = null;
        try {
            DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
            date = new java.sql.Date(((java.util.Date) formatter.parse(data)).getTime());
        } catch (ParseException e) {
            throw e;
        }
        return date;
    }

    
    public static java.sql.Time formataHora(String data) throws Exception {
        if (data == null || data.equals("")) {
            return null;
        }

        java.sql.Time date = null;
        try {
            DateFormat formatter = new SimpleDateFormat("HH:mm");
            date = new java.sql.Time(((java.util.Date) formatter.parse(data)).getTime());
        } catch (ParseException e) {
            throw e;
        }
        return date;
    }

    
}
